<?php

/**
 * Created by PhpStorm.
 * User: liupengfei
 * Date: 2017/9/5
 * Time: 10:23
 */
require_once 'Config.php';

class Model
{
    protected $pOb;//pdo对象
    protected $tableName = "";

    function __construct($tableName = "")
    {
        //实例化pdo对象
        $pdo = new PDO("mysql:host=" . Config::DB_HOST . ";dbname=" . Config::DB_NAME, Config::DB_USER, Config::DB_PASS);
        //设置交互字符集
        $pdo->exec("set names " . Config::DB_CHARSET);
        $this->pOb = $pdo;
        $this->tableName = $tableName;
    }

    function add($arr)
    {
        if (is_array($arr) && !empty($arr)) {
            //由数组$arr 产生 inert语句
            $fieldStr = "";
            $valueStr = "";
            foreach ($arr as $k => $v) {
                $fieldStr .= "," . $k;
                $valueStr .= ",'" . $v . "'";
            }
            $fieldStr = substr($fieldStr, 1);
            $valueStr = substr($valueStr, 1);
            $sql = "insert into {$this->tableName}({$fieldStr}) values({$valueStr})";

            //执行sql
            $re = $this->pOb->exec($sql);
            if ($re) {
                //返回主键id值
                return $this->pOb->lastInsertId();
            } else {
                return false;
            }

        } else {
            return false;
        }
    }

    //修改
    function save($arr, $where = "")
    {
        //update 表名 set title=值   where $where
        $where = !empty($where) ? "where " . $where : "";
        if (is_array($arr) && !empty($arr)) {
            $fvStr = "";
            foreach ($arr as $k => $v) {
                $fvStr .= ",{$k}='{$v}'";
            }
            $fvStr = substr($fvStr, 1);
            $sql = "update {$this->tableName} set {$fvStr} $where";
            //执行update语句
            $re = $this->pOb->exec($sql);
            //返回影响记录的条数
            return $re;
        } else {
            return false;
        }
    }

    //删除
    function delete($where = "")
    {
        $where = !empty($where) ? "where " . $where : "";
        $sql = "delete from {$this->tableName} " . $where;
        return $this->pOb->exec($sql);
    }

    //查询
    function select($fields = "*", $where = "", $limit = "", $order = "")
    {
        //select * from $this->tableName where group by having order by limit
        $where = !empty($where) ? "where " . $where : "";
        $limit = !empty($limit) ? "limit " . $limit : "";
        $order = !empty($order) ? "order by " . $order : "";
        $sql = "select {$fields} from {$this->tableName} $where $order $limit";
        //返回二维数组
        return $this->pOb->query($sql)->fetchAll(PDO::FETCH_ASSOC);
    }

    //总条数
    function count($where = "")
    {
        $where = !empty($where) ? "where " . $where : "";
        $sql = "select count(*) as num from {$this->tableName} $where";
        $arr = $this->pOb->query($sql)->fetch();
        if (is_array($arr) && !empty($arr)) {
            return $arr[0];
        } else {
            return false;
        }
    }

    //获取一条记录
    function getOne($where = "")
    {// id=5
        $where = !empty($where) ? "where " . $where : "";
        $sql = "select * from {$this->tableName} $where limit 1";
        return $this->pOb->query($sql)->fetch(PDO::FETCH_ASSOC);
    }

    //执行sql语句
    function query($sql)
    {
        //select 返回二维数组，delete update 影响记录条数  insert 返回主键id值
        //判断sql语句的类型
        if (preg_match("/^select/i", trim($sql))) {
            //select语句
            $pdoS = $this->pOb->query($sql);
            if (is_object($pdoS)) {
                $pdoS->setFetchMode(PDO::FETCH_ASSOC);//关联下标
                return $pdoS->fetchAll();
            } else {
                return false;
            }
        } else if (preg_match("/^insert/i", trim($sql))) {
            $re = $this->pOb->exec($sql);
            if ($re) {
                //返回主键id值
                return $this->pOb->lastInsertId();
            } else {
                return false;
            }
        } else {
            return $this->pOb->exec($sql);
        }
    }
}